﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Text;
using System.Web.Script.Serialization;
using System.Drawing;
using System.Windows.Forms;

namespace EasyClass {
	public class Problem {
		static private JavaScriptSerializer jss = new JavaScriptSerializer();
		public Task task;
		public string description;
		public int isSingle;  //1:single choice 0:uncertain
		public string[] choices;
		public string answer;

		public Problem(Task t, string d, string[] c, string a, int i) {
			task = t;
			description = d;
			choices = c;
			answer = a;
			isSingle = i;
		}

		public Panel GetPanel(int idx) {
			FlowLayoutPanel p1 = new FlowLayoutPanel();
			p1.FlowDirection = FlowDirection.TopDown;
			p1.WrapContents = false;
			p1.Width = 300;
			p1.Dock = DockStyle.Left;
			Label labeld = new Label();
			labeld.AutoSize = true;
			labeld.Text = idx.ToString() + ". " + description;
			labeld.Width = 300;
			p1.Controls.Add(labeld);
			int height = labeld.Height;
			for (int i = 0; i < choices.Length; ++i) {
				Label l = new Label();
				l.AutoSize = true;
				char ch = Convert.ToChar('A' + i);
				if (answer.IndexOf(ch) >= 0) {
					l.ForeColor = Color.Red;
				}
				l.Text = "  " + ch.ToString() + ". " + choices[i];
				l.Width = 300;
				p1.Controls.Add(l);
				height += l.Height;
			}
			p1.Height = height;

			FlowLayoutPanel p2 = new FlowLayoutPanel();
			p2.FlowDirection = FlowDirection.TopDown;
			p2.Width = 50;
			p2.Dock = DockStyle.Right;
			ProblemButton button0 = new ProblemButton(this);
			button0.Width = 50;
			button0.Text = "Edit";
			button0.Click += new EventHandler(task.EditButton_Click);
			ProblemButton button1 = new ProblemButton(this);
			button1.Width = 50;
			button1.Text = "Delete";
			button1.Click += new EventHandler(task.DeleteButton_Click);
			p2.Controls.Add(button0);
			p2.Controls.Add(button1);

			Panel panel = new Panel();
			panel.Width = 375;
			panel.Height = Math.Max(p1.Height, p2.Height);
			panel.Controls.Add(p1);
			panel.Controls.Add(p2);
			return panel;
		}

		public string ToJSON() {
			Dictionary<string, string> dict = new Dictionary<string, string>();
			dict["Description"] = description;
			dict["IsSingle"] = isSingle.ToString();
			dict["Answer"] = answer;
			dict["NChoices"] = choices.Length.ToString();
			int cnt = 0;
			foreach (string s in choices) {
				dict["Choice" + cnt++.ToString()] = s;
			}
			return jss.Serialize(dict);
		}

		static public Problem deJSON(Task task, string content) {
			Dictionary<string, string> dict = jss.Deserialize<Dictionary<string, string>>(content);
			int nChoices = Int32.Parse(dict["NChoices"]);
			string[] choices = new string[nChoices];
			for (int i = 0; i < nChoices; ++i)
				choices[i] = dict["Choice" + i.ToString()];
			Problem problem = new Problem(task, dict["Description"], choices, dict["Answer"], Int32.Parse(dict["IsSingle"]));
			return problem;
		}
	}
}
